#include "sizepagecombox.h"

#include <QStyleOption>
#include <QRegularExpressionValidator>
#include <QRegularExpression>

#include <QPainter>
#include <QListView>
#include "../../uitools/vhc/constview4fun.h"
#include "../../uitools/vhc/constview4id.h"

namespace kepen {
namespace core {

SizePageCombox::SizePageCombox(QWidget *parent)
    : QWidget(parent)
{
    QString nameThiz("per_page_item");
    QString cssThiz = QString("QWidget#%1{background-color:transparent;font-size:%2px;}").arg(nameThiz).arg(TextSize_ToolBar);
    QString cssCombox=QString("QComboBox{font-size:%1px;border:1px solid #cdcdcd;color: #5e5e5e;}QComboBox:hover{border:1px solid #666;color:black;}QComboBox QAbstractItemView{text-align:center;font-weight:bold;border:none;font-size:%2px;outline:0px;}")
                            .arg(TextSize_ToolBar).arg(TextSize_ToolBar);
    setObjectName(nameThiz);
    setStyleSheet(cssThiz);
    m_pCbx = new QComboBox(this);
    m_pCbx->setFixedSize(60, 27);
    setFixedSize(65, 30);
    m_pCbx->setView(new QListView(this));
    m_pCbx->view()->setFocusPolicy(Qt::NoFocus);

    m_pCbx->setStyleSheet(cssCombox);
    //   "QComboBox::down-arrow{image:url(:/pagination/skin/drop_down.png);}"
    // "QComboBox::down-arrow:on{image:url(:/pagination/skin/drop_up.png);}"
    // m_pCbx->setStyleSheet(QStringLiteral("QComboBox{text-align:center;font-size:15px;border:1px solid #DCDFE6;border-radius:5px;color: #666;padding: 1px 2px 1px 2px;min-width: 60px;}"
    //                                      "QComboBox:hover{border:1px solid #409EFF;}"
    //                                      "QComboBox::drop-down{background-color:transparent;subcontrol-origin: padding;subcontrol-position: top right;width: 20px;border: none;}"
    //                                      "QComboBox QAbstractItemView{text-align:center;font-weight:bold;border:none;font-size:14px;outline:0px;}"
    //                                      "QComboBox QAbstractItemView::item{border:none;color:#606266;border:none;height:30px;}"
    //                                      "QComboBox QAbstractItemView::item:selected{color:#409EFF;background:rgb(245,247,250);}"));
    m_pCbx->setCursor(QCursor(Qt::PointingHandCursor));
    m_pCbx->move(width() / 2 - m_pCbx->width() / 2, height() / 2 - m_pCbx->height() / 2);
    m_vecPage.reserve(5);

    connect(m_pCbx, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [&](int index){
        emit EachPageItemChanged(m_vecPage[index]);
    });
}

SizePageCombox::~SizePageCombox()
{

}

void SizePageCombox::SetSizePageData(int *array, int size)
{
    m_vecPage.clear();
    m_pCbx->clear();
    m_vecPage.reserve(5);
    for(int i = 0; i < size; i++)
    {
        int n = array[i];
        if(n > 0)
        {
            if(i > 0)
            {
                if(n <= m_vecPage[i - 1]) continue;
            }
            m_vecPage.emplace_back(n);
            m_pCbx->addItem(QString::number(n));
        }
    }
}

int SizePageCombox::GetSizePage()
{
    int idx = m_pCbx->currentIndex();
    if(idx>=0 && idx < m_vecPage.size())
    {
        return m_vecPage[idx];
    }
    else return 0;
}



void SizePageCombox::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(event);
}


}
}
